/******************************************************************************************************
Copyright (C) 2013 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/

/*globals Env, draw, mainWindow, qsTr, Settings, Application, GraphReporterBase*/
/*jslint plusplus: true, indent: 4, loopfunc: true*/

function GraphReporterTest() {
	"use strict";
	
	this.fName = "Report Fit Parameters";
}

GraphReporterTest.prototype = new GraphReporterBase();

GraphReporterTest.prototype.report = function (aGraph, aCreateItem, aHtml, aSettings) {
	'use strict';
	var sCount, s, fitModels, yModels, reportText, graphProperties, horzAxsLabel, 
		yModelLabel, formula, parameters, template, parametersStr, p, parameterTemplate,
		htmlTemplate, reportParamTemplate, reportParams;

	if (aGraph === undefined || !aGraph.isValid()) {
		throw new Error("Invalid Graph");
	}

	htmlTemplate = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><title>Graph Report</title></head><body>%1</body></html>"; //%1 html body
	if (aHtml) {
		template = "<p>%1</p>"; //%1: label, %2: formula, %3: parameters, %4: comments
		parameterTemplate = '<p>%1=%2</p>'; //%1: name, %2: value
		reportParamTemplate = "<p>%1(%2)=%3</p>"; //%1: name, %2: formula, %3 value
	} else {
		template = "%1\n";
		parameterTemplate = '%1=%2\n'; //%1: name, %2: value
		reportParamTemplate = "%1(%2)=%3\n"; //%1: name, %2: formula, %3 value
	}
	
	reportText = '';
	sCount = aGraph.seriesCount;
	fitModels = aGraph.fitModels;
	yModels = aGraph.yModels;
	graphProperties = aGraph.graphicProperties;
	horzAxsLabel = graphProperties.horzAxis.label;
	horzAxsLabel = horzAxsLabel.replace(/\([\w\W]*\)/, '');
	for (s = 0; s < sCount; ++s) {
		yModelLabel = yModels[s].label;
		if (yModelLabel.length === 0) {
			if (s === 0) {
				yModelLabel = 'Y';
			} else {
				yModelLabel = 'Y%1'.arg(s);
			}
		}
		formula = fitModels[s].formula;
		if (formula.length !== 0) {
			formula = formula.replace(/exp/g, 'EXP');
			formula = formula.replace(/x/g, horzAxsLabel);
			formula = formula.replace(/EXP/g, 'exp');
			parameters = fitModels[s].fitParams;
			parametersStr = '';
			Object.keys(parameters).forEach(function (p) {
				if (parametersStr.length !== 0) {
					parametersStr += '\n';
				}
				parametersStr += parameterTemplate.arg(p).arg(parameters[p].toPrecision(aSettings.fDecimals));
			});
			parameters = fitModels[s].reportParams;
			Object.keys(parameters).forEach(function (p) {
				if (parametersStr.length !== 0) {
					parametersStr += ' ';
				}
				parametersStr += reportParamTemplate.arg(p).arg(parameters[p].Formula).arg(parameters[p].Value.toPrecision(aSettings.fDecimals));
			});
			// reportText += template.arg(yModelLabel).arg(formula).arg(parametersStr).arg(fitModels[s].error.toPrecision(precision));
			reportText += template.arg(parametersStr);
		}
	}
	if (aHtml) {
		reportText = htmlTemplate.arg(reportText);
	}
	if (aCreateItem) {
		draw.text(reportText, "Report Special", "Graph", true, aGraph.uuid);
	}
	return reportText;
};

Env.GraphReporters.push(new GraphReporterTest());